package com.css.bjdt_core.reportFill.service;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.Sql;
import org.nutz.dao.sql.SqlCallback;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.css.bjdt_core.cell.entity.Cell;
import com.css.bjdt_core.cell.entity.Cell.CellType;
import com.css.bjdt_core.dnDzz.entity.DnDzz;
import com.css.bjdt_core.dnDzz.service.DnDzzService;
import com.css.bjdt_core.reportFill.entity.CellDataValue;
import com.css.bjdt_core.reportPrepare.entity.RptBatch;
import com.css.bjdt_core.reportPrepare.entity.RptUnit;
import com.css.bjdt_core.reportPrepare.service.RptDealWithService;
import com.css.bjdt_core.reportPrepare.service.RptUnitService;
import com.css.bjdt_core.util.ReportConstant;
import com.css.bjdt_core.util.TimeUtil;

//历史报表填充方法
@Service
public class ReportHistoryService {

	@Autowired
	private Dao dao;

	@Autowired
	private RptUnitService rptUnitService;

	@Autowired
	private RptDealWithService rptDealWithService;

	@Autowired
	private DnDzzService dnDzzService;

	//非汇总数据读取
	public List<CellDataValue> getAllCellDataValueNewHistory(String annualId, String templateId, String batchId,
			String dzzId) {
		//去掉老的方法，留新的方法，等系统稳定了，再把老方法都删掉，心里没底，新方法有没有BUG
		Map<String, CellDataValue> cellDataValueMap = new HashMap<String, CellDataValue>();
		List<Cell> cellList = dao.query(Cell.class, Cnd.where("TEMPLATE_ID", "=", templateId));
		RptUnit rptUnit = rptUnitService.queryByDzzIdAndAnnualId(dzzId, annualId);
		DnDzz dnDzz = dnDzzService.getDnDzzDataByDzzId(dzzId);
		RptBatch rptBatch = rptDealWithService.queryBatch(batchId);
		for (Cell cell : cellList) {
			String mapKey = cell.getAbsRowNum() + "--" + cell.getAbsColNum();
			CellDataValue cellDataValue = new CellDataValue();
			cellDataValue.setBatchId(batchId);
			cellDataValue.setAbsRowNum(cell.getAbsRowNum() + "");
			cellDataValue.setAbsColNum(cell.getAbsColNum() + "");
			cellDataValue.setRelRowNum(cell.getRelRowNum() + "");
			cellDataValue.setRelColNum(cell.getRelColNum() + "");
			cellDataValue.setDzzId(dzzId);
			cellDataValue.setTemplateId(templateId);
			cellDataValue.setCellType(cell.getCellType());
			cellDataValue.setIsHaveComment(cell.getIsComment());
			//判断该指标是不是为空，如果为空就给它赋值--，如果他是预设类指标，就直接给他赋相对的预设类指标
			if (cell.getCellType().equals(CellType.cellNull.getKey())) {
				cellDataValue.setCellValue("--");
			} else if (cell.getCellType().equals(CellType.quote.getKey())) {
				switch (cell.getQuoteType()) {
				case "1":
					if (null == rptUnit) {
						cellDataValue.setCellValue("填报单位：" + dnDzz.getDzzmc());
					} else {
						cellDataValue.setCellValue("填报单位：" + rptUnit.getFullName());
					}
					break;
				case "2":
					cellDataValue.setCellValue("截止时间：" + TimeUtil.dateFormatString(rptBatch.getEndTime()));
					break;
				case "3":
					cellDataValue.setCellValue("开始时间：" + TimeUtil.dateFormatString(rptBatch.getStartTime()));
					break;
				case "4":
					cellDataValue.setCellValue("起止时间：" + TimeUtil.dateFormatString(rptBatch.getStartTime()) + "至"
							+ TimeUtil.dateFormatString(rptBatch.getEndTime()));
					break;
				case "5":
					if (null == rptUnit) {
						cellDataValue.setCellValue("");
					} else {
						cellDataValue.setCellValue(rptUnit.getRelMan());
					}
					break;
				default:
					break;
				}
			} else if (cell.getCellType().equals(CellType.input.getKey())) {
				cellDataValue.setCellValue("0");
			} else {
			}
			cellDataValueMap.put(mapKey, cellDataValue);
		}

		String tableName = ReportConstant.EXECUTIVE_TABLE_PREFIX + annualId;
		Sql strSql = Sqls
				.create("select CELL_DATA_ID,CELL_VALUE,BATCH_ID,ABS_ROW_NUM,ABS_COL_NUM,REL_ROW_NUM,REL_COL_NUM,DZZ_ID,TEMPLATE_ID,HISTORY_CELL_VALUE,SUM_STATUS,BATCH_ID from "
						+ tableName + " where DZZ_ID = @dzzId and TEMPLATE_ID = @templateId order by CELL_VALUE");
		strSql.params().set("dzzId", dzzId).set("templateId", templateId);
		strSql.setCallback(new SqlCallback() {
			@Override
			public Object invoke(Connection conn, ResultSet rs, Sql strSql) throws SQLException {
				List<CellDataValue> cellDataValueList = new ArrayList<CellDataValue>();
				while (rs.next()) {
					CellDataValue cellDataValue = new CellDataValue();
					cellDataValue.setAbsColNum(rs.getString("abs_col_num"));
					cellDataValue.setAbsRowNum(rs.getString("abs_row_num"));
					cellDataValue.setCellValue(rs.getString("CELL_VALUE"));
					cellDataValue.setCellDataId(rs.getString("CELL_DATA_ID"));
					cellDataValue.setHistoryCellValue(rs.getString("HISTORY_CELL_VALUE"));
					cellDataValue.setSumStatus(rs.getString("SUM_STATUS"));
					cellDataValue.setBatchId(rs.getString("BATCH_ID"));
					cellDataValueList.add(cellDataValue);
				}
				return cellDataValueList;
			}
		});
		dao.execute(strSql);
		List<CellDataValue> cdv = strSql.getList(CellDataValue.class);
		if (cdv != null && cdv.size() != 0) {
			for (CellDataValue cellDataValue : cdv) {
				String key = cellDataValue.getAbsRowNum() + "--" + cellDataValue.getAbsColNum();
				CellDataValue cellDataValueNew = cellDataValueMap.get(key);
				cellDataValueNew.setCellValue(cellDataValue.getCellValue());
				cellDataValueNew.setCellDataId(cellDataValue.getCellDataId());
				cellDataValueNew.setHistoryCellValue(cellDataValue.getHistoryCellValue());
				cellDataValueMap.put(key, cellDataValueNew);
			}
		}
		//最后把MAP转换成LIST，返回
		List<CellDataValue> cellDataValueListJsp = new ArrayList<CellDataValue>();
		for (String key : cellDataValueMap.keySet()) {
			CellDataValue cellDataValueNew = cellDataValueMap.get(key);
			cellDataValueListJsp.add(cellDataValueNew);
		}
		return cellDataValueListJsp;
	}
}
